﻿using System;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Rendering;

namespace JQMergeFramework
{
    public class UICityCamera: MonoBehaviour
    {
        public static UICityCamera I;
        public Transform cityNode;
        public Transform effectNode;

        private List<CityUnit> cityList = new List<CityUnit>();
        private void Awake()
        {
            I = this;
        }
        
        
        public void ShowCityNode(int lv,Action call)
        {
            DOTween.Kill(this.GetInstanceID());
            
            CityUnit cityUnit =  TowerControl.I.LoadCityObj(lv);
            cityList.Add(cityUnit);
            //合成目标动画
            cityUnit.mMeshRenderer.shadowCastingMode  = ShadowCastingMode.Off;
            
            cityUnit.transform.SetParent(cityNode);
            cityUnit.transform.localPosition = new Vector3(0,-cityUnit.mMeshSize.y/2,0);
            cityUnit.transform.localScale = Vector3.one;

            cityUnit.transform.localRotation = Quaternion.Euler(new Vector3(-10,0,0));
            cityUnit.mMeshRenderer.transform.DOLocalRotate(new Vector3(0, 360, 0), 1.5F, RotateMode.LocalAxisAdd)
                .SetEase(Ease.Linear).SetLoops(-1);
            
            Sequence sq =  DOTween.Sequence(this.GetInstanceID());
            sq.Append(cityNode.DOScale(Vector3.one, 0.5F).SetEase(Ease.OutBack));
            sq.Join(effectNode.DOScale(Vector3.one, 0.3F).SetEase(Ease.OutBack));
            sq.Play();
        }


        public void HideCityNode()
        {
            DOTween.Kill(this.GetInstanceID());
            
            Sequence sq =  DOTween.Sequence(this.GetInstanceID());
            sq.Append(cityNode.DOScale(Vector3.zero, 0.3F).SetEase(Ease.Linear));
            sq.Join(effectNode.DOScale(Vector3.zero, 0.3F).SetEase(Ease.Linear));
            sq.OnComplete(() =>
            {
                for (int i = 0; i < cityList.Count; i++)
                {
                    cityList[i].RDestroy();
                }
                cityList.Clear();
            });
            sq.Play();
        }

    }
}